package com.jcms.cms.action.admin.main;

import static com.jcms.common.page.SimplePage.cpn;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.jcms.cms.entity.parcel.Order;
import com.jcms.cms.manager.parcel.OrderManager;
import com.jcms.common.page.Pagination;
import com.jcms.common.web.CookieUtils;

@Controller
public class ParcelAct {
	private static final Logger log = LoggerFactory.getLogger(ParcelAct.class);
	
	@Autowired
	private OrderManager orderManager;
	
	@RequestMapping(value = "/parcel/order_list.do")
	public String list(Integer pageNo, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		Pagination pagination = orderManager.getPage(cpn(pageNo),
				CookieUtils.getPageSize(request));
		model.addAttribute("pagination", pagination);
		model.addAttribute("pageNo", pagination.getPageNo());
		return "parcel/order_list";
	}
	
	@RequestMapping(value = "/parcel/order_edit.do")
	public String edit(@RequestParam("id") Integer id, Integer pageNo, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		Order order = orderManager.getOrderById(id);
		model.addAttribute("pageNo", pageNo);
		model.addAttribute("order", order);
		return "parcel/order_edit";
	}
	
	@RequestMapping(value = "/parcel/order_update.do")
	public String update(String href, @RequestParam("id") Integer id, Integer pageNo, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		Order order = orderManager.getOrderById(id);
		order.setHref(href);
		orderManager.updateOrder(order);
		model.addAttribute("pageNo", pageNo);
		return list(pageNo, request, response, model);
	}
	
	@RequestMapping(value = "/parcel/order_view.do",method=RequestMethod.GET)
	public String view(@RequestParam("id") Integer id, Integer pageNo, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		Order order = orderManager.getOrderById(id);
		model.addAttribute("pageNo", pageNo);
		model.addAttribute("order", order);
		return "parcel/order_view";
	}
}
